package org.openwork.portlet.skillmanagement.service;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.BaseModel;

import org.openwork.portlet.skillmanagement.model.SkillCategoryClp;
import org.openwork.portlet.skillmanagement.model.SkillClp;
import org.openwork.portlet.skillmanagement.model.SkillUserClp;

import java.lang.reflect.Method;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class ClpSerializer {
    public static final String SERVLET_CONTEXT_NAME = "openwork";
    private static Log _log = LogFactoryUtil.getLog(ClpSerializer.class);
    private static ClassLoader _classLoader;

    public static void setClassLoader(ClassLoader classLoader) {
        _classLoader = classLoader;
    }

    public static Object translateInput(BaseModel<?> oldModel) {
        Class<?> oldModelClass = oldModel.getClass();

        String oldModelClassName = oldModelClass.getName();

        if (oldModelClassName.equals(SkillCategoryClp.class.getName())) {
            SkillCategoryClp oldCplModel = (SkillCategoryClp) oldModel;

            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    Class<?> newModelClass = Class.forName("org.openwork.portlet.skillmanagement.model.impl.SkillCategoryImpl",
                            true, _classLoader);

                    Object newModel = newModelClass.newInstance();

                    Method method0 = newModelClass.getMethod("setId",
                            new Class[] { Long.TYPE });

                    Long value0 = new Long(oldCplModel.getId());

                    method0.invoke(newModel, value0);

                    Method method1 = newModelClass.getMethod("setGroupId",
                            new Class[] { Long.TYPE });

                    Long value1 = new Long(oldCplModel.getGroupId());

                    method1.invoke(newModel, value1);

                    Method method2 = newModelClass.getMethod("setUserId",
                            new Class[] { Long.TYPE });

                    Long value2 = new Long(oldCplModel.getUserId());

                    method2.invoke(newModel, value2);

                    Method method3 = newModelClass.getMethod("setCreateDate",
                            new Class[] { Date.class });

                    Date value3 = oldCplModel.getCreateDate();

                    method3.invoke(newModel, value3);

                    Method method4 = newModelClass.getMethod("setModifiedDate",
                            new Class[] { Date.class });

                    Date value4 = oldCplModel.getModifiedDate();

                    method4.invoke(newModel, value4);

                    Method method5 = newModelClass.getMethod("setName",
                            new Class[] { String.class });

                    String value5 = oldCplModel.getName();

                    method5.invoke(newModel, value5);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        if (oldModelClassName.equals(SkillClp.class.getName())) {
            SkillClp oldCplModel = (SkillClp) oldModel;

            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    Class<?> newModelClass = Class.forName("org.openwork.portlet.skillmanagement.model.impl.SkillImpl",
                            true, _classLoader);

                    Object newModel = newModelClass.newInstance();

                    Method method0 = newModelClass.getMethod("setId",
                            new Class[] { Long.TYPE });

                    Long value0 = new Long(oldCplModel.getId());

                    method0.invoke(newModel, value0);

                    Method method1 = newModelClass.getMethod("setCategoryId",
                            new Class[] { Long.TYPE });

                    Long value1 = new Long(oldCplModel.getCategoryId());

                    method1.invoke(newModel, value1);

                    Method method2 = newModelClass.getMethod("setUserId",
                            new Class[] { Long.TYPE });

                    Long value2 = new Long(oldCplModel.getUserId());

                    method2.invoke(newModel, value2);

                    Method method3 = newModelClass.getMethod("setCreateDate",
                            new Class[] { Date.class });

                    Date value3 = oldCplModel.getCreateDate();

                    method3.invoke(newModel, value3);

                    Method method4 = newModelClass.getMethod("setModifiedDate",
                            new Class[] { Date.class });

                    Date value4 = oldCplModel.getModifiedDate();

                    method4.invoke(newModel, value4);

                    Method method5 = newModelClass.getMethod("setName",
                            new Class[] { String.class });

                    String value5 = oldCplModel.getName();

                    method5.invoke(newModel, value5);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        if (oldModelClassName.equals(SkillUserClp.class.getName())) {
            SkillUserClp oldCplModel = (SkillUserClp) oldModel;

            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    Class<?> newModelClass = Class.forName("org.openwork.portlet.skillmanagement.model.impl.SkillUserImpl",
                            true, _classLoader);

                    Object newModel = newModelClass.newInstance();

                    Method method0 = newModelClass.getMethod("setUserId",
                            new Class[] { Long.TYPE });

                    Long value0 = new Long(oldCplModel.getUserId());

                    method0.invoke(newModel, value0);

                    Method method1 = newModelClass.getMethod("setSkillId",
                            new Class[] { Long.TYPE });

                    Long value1 = new Long(oldCplModel.getSkillId());

                    method1.invoke(newModel, value1);

                    Method method2 = newModelClass.getMethod("setAuditUserId",
                            new Class[] { Long.TYPE });

                    Long value2 = new Long(oldCplModel.getAuditUserId());

                    method2.invoke(newModel, value2);

                    Method method3 = newModelClass.getMethod("setCreateDate",
                            new Class[] { Date.class });

                    Date value3 = oldCplModel.getCreateDate();

                    method3.invoke(newModel, value3);

                    Method method4 = newModelClass.getMethod("setModifiedDate",
                            new Class[] { Date.class });

                    Date value4 = oldCplModel.getModifiedDate();

                    method4.invoke(newModel, value4);

                    Method method5 = newModelClass.getMethod("setExperience",
                            new Class[] { Short.TYPE });

                    Short value5 = new Short(oldCplModel.getExperience());

                    method5.invoke(newModel, value5);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        return oldModel;
    }

    public static Object translateInput(List<Object> oldList) {
        List<Object> newList = new ArrayList<Object>(oldList.size());

        for (int i = 0; i < oldList.size(); i++) {
            Object curObj = oldList.get(i);

            newList.add(translateInput(curObj));
        }

        return newList;
    }

    public static Object translateInput(Object obj) {
        if (obj instanceof BaseModel<?>) {
            return translateInput((BaseModel<?>) obj);
        } else if (obj instanceof List<?>) {
            return translateInput((List<Object>) obj);
        } else {
            return obj;
        }
    }

    public static Object translateOutput(BaseModel<?> oldModel) {
        Class<?> oldModelClass = oldModel.getClass();

        String oldModelClassName = oldModelClass.getName();

        if (oldModelClassName.equals(
                    "org.openwork.portlet.skillmanagement.model.impl.SkillCategoryImpl")) {
            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    SkillCategoryClp newModel = new SkillCategoryClp();

                    Method method0 = oldModelClass.getMethod("getId");

                    Long value0 = (Long) method0.invoke(oldModel,
                            (Object[]) null);

                    newModel.setId(value0);

                    Method method1 = oldModelClass.getMethod("getGroupId");

                    Long value1 = (Long) method1.invoke(oldModel,
                            (Object[]) null);

                    newModel.setGroupId(value1);

                    Method method2 = oldModelClass.getMethod("getUserId");

                    Long value2 = (Long) method2.invoke(oldModel,
                            (Object[]) null);

                    newModel.setUserId(value2);

                    Method method3 = oldModelClass.getMethod("getCreateDate");

                    Date value3 = (Date) method3.invoke(oldModel,
                            (Object[]) null);

                    newModel.setCreateDate(value3);

                    Method method4 = oldModelClass.getMethod("getModifiedDate");

                    Date value4 = (Date) method4.invoke(oldModel,
                            (Object[]) null);

                    newModel.setModifiedDate(value4);

                    Method method5 = oldModelClass.getMethod("getName");

                    String value5 = (String) method5.invoke(oldModel,
                            (Object[]) null);

                    newModel.setName(value5);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        if (oldModelClassName.equals(
                    "org.openwork.portlet.skillmanagement.model.impl.SkillImpl")) {
            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    SkillClp newModel = new SkillClp();

                    Method method0 = oldModelClass.getMethod("getId");

                    Long value0 = (Long) method0.invoke(oldModel,
                            (Object[]) null);

                    newModel.setId(value0);

                    Method method1 = oldModelClass.getMethod("getCategoryId");

                    Long value1 = (Long) method1.invoke(oldModel,
                            (Object[]) null);

                    newModel.setCategoryId(value1);

                    Method method2 = oldModelClass.getMethod("getUserId");

                    Long value2 = (Long) method2.invoke(oldModel,
                            (Object[]) null);

                    newModel.setUserId(value2);

                    Method method3 = oldModelClass.getMethod("getCreateDate");

                    Date value3 = (Date) method3.invoke(oldModel,
                            (Object[]) null);

                    newModel.setCreateDate(value3);

                    Method method4 = oldModelClass.getMethod("getModifiedDate");

                    Date value4 = (Date) method4.invoke(oldModel,
                            (Object[]) null);

                    newModel.setModifiedDate(value4);

                    Method method5 = oldModelClass.getMethod("getName");

                    String value5 = (String) method5.invoke(oldModel,
                            (Object[]) null);

                    newModel.setName(value5);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        if (oldModelClassName.equals(
                    "org.openwork.portlet.skillmanagement.model.impl.SkillUserImpl")) {
            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    SkillUserClp newModel = new SkillUserClp();

                    Method method0 = oldModelClass.getMethod("getUserId");

                    Long value0 = (Long) method0.invoke(oldModel,
                            (Object[]) null);

                    newModel.setUserId(value0);

                    Method method1 = oldModelClass.getMethod("getSkillId");

                    Long value1 = (Long) method1.invoke(oldModel,
                            (Object[]) null);

                    newModel.setSkillId(value1);

                    Method method2 = oldModelClass.getMethod("getAuditUserId");

                    Long value2 = (Long) method2.invoke(oldModel,
                            (Object[]) null);

                    newModel.setAuditUserId(value2);

                    Method method3 = oldModelClass.getMethod("getCreateDate");

                    Date value3 = (Date) method3.invoke(oldModel,
                            (Object[]) null);

                    newModel.setCreateDate(value3);

                    Method method4 = oldModelClass.getMethod("getModifiedDate");

                    Date value4 = (Date) method4.invoke(oldModel,
                            (Object[]) null);

                    newModel.setModifiedDate(value4);

                    Method method5 = oldModelClass.getMethod("getExperience");

                    Short value5 = (Short) method5.invoke(oldModel,
                            (Object[]) null);

                    newModel.setExperience(value5);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        return oldModel;
    }

    public static Object translateOutput(List<Object> oldList) {
        List<Object> newList = new ArrayList<Object>(oldList.size());

        for (int i = 0; i < oldList.size(); i++) {
            Object curObj = oldList.get(i);

            newList.add(translateOutput(curObj));
        }

        return newList;
    }

    public static Object translateOutput(Object obj) {
        if (obj instanceof BaseModel<?>) {
            return translateOutput((BaseModel<?>) obj);
        } else if (obj instanceof List<?>) {
            return translateOutput((List<Object>) obj);
        } else {
            return obj;
        }
    }
}
